/***************************************************************************
 *   Copyright (C) 2009 by maarten,,,   *
 *   maarten@maarten-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef OBJECTPLANE_H
#define OBJECTPLANE_H

#include <QVector>

namespace draw
{
 class Ray; 
 class Box;
 class HitRecord;
}
namespace scene
{
  class Material;
  class Texture;
};
namespace object {

class Vertex;
class ObjectTexture;
class Normal;

/**
	@author maarten,,, <maarten@maarten-laptop>
*/
class Plane{
public:
    Plane();
    Plane(object::Vertex *va, object::Vertex *vb, object::Vertex *vc);
    ~Plane();

    void addCorner(Vertex *vertex = NULL,
                   ObjectTexture *texture  = NULL,
                   Normal *normal  = NULL);

    Vertex* getVertex(int id){return m_vertexList[id];} // TODO check for size
    void print();
    ObjectTexture* getTextureCoord(int id){return m_textureList[id];}
    Vertex* getSurfaceNormal(float b = -1.0, float g = -1.0);
    
    void setMat(scene::Material * mat){m_mat = mat;};
    void setTex(scene::Texture * tex){m_tex = tex;};
    
    scene::Material* getMat(){return m_mat;}
    scene::Texture* getTex(){return m_tex;}
    
    // returns negative if on left side, positive if on rightside,
    // 0 if on plane
    float planeEquation(object::Vertex * p);
    
    virtual draw::Box* getBoundingBox(){return bbox;}
    virtual void setBoundingBox();
   
    inline float getCenterX() const {return xCenter;}
    inline float getCenterY() const {return yCenter;}
    inline float getCenterZ() const {return zCenter;}
    
    virtual bool hit(draw::Ray * ray, draw::HitRecord ** rec);
    virtual bool hit(draw::Ray * ray, float tMax);
    
    void calculateNormal();
    
private:
    QVector<Vertex*> m_vertexList;
    QVector<ObjectTexture*> m_textureList;
    QVector<Normal*> m_normalList;
    
    Vertex* surfaceNormal;
    Vertex* specificNormal;
    
    bool m_normalsGiven;
    scene::Material * m_mat;
    scene::Texture * m_tex;
    
    
    
    float Aa;
    float Ad;
    float Ab;
    float Ae;
    float Ac;
    float Af; 
   
protected:
  draw::Box *bbox;
  float xCenter;
  float yCenter;
  float zCenter;

};

}

#endif
